// SimNet is a Second Life(tm) sim mangement tool that was designed for Fermi Sandbox
// Copyright (C) 2011 Arthur Fermi
// 
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// To receive a copy of this license please visit http://www.gnu.org/licenses/

// SimNet - Home

// Global Variables
    key     gkeyAvatar;         // Who is in trouble
    list    glstAVList;         // Avatar List
    integer gintTempListen;     // Temp Listen Device
    integer gintTempDCC;        // Temp Dialog Listen
    integer gintDCC;            // Dialog Communications Channel
    string  gstrHomeMessage;     // Message to send to to the person sent home
    
// Read in messages
string  gstrURL;
string  gstrHTTP;

    
// Functions
funScan()
{
    // Run a sensor scan
    llSensor("", NULL_KEY, AGENT, 96, PI);
}    

funListAVs()
{
    if (llGetListLength(glstAVList) == 0)
    {
        llOwnerSay("You are the only avatar with in 96m, I don't think you want to send yourself home, so I'm turning off.");
        
          // Clean out listen
        llListenRemove(gintTempListen);
        llSetTimerEvent(0);   
    
    }
    else
    {        
        //Only Listen for 30 seconds
        llSetTimerEvent(30);
        
        //Setup Listen
        gintTempListen = llListen(0, "", llGetOwner(), "");
            
        // Break down List
        integer intNumAVs = llGetListLength(glstAVList);
        
        string AvList = " \n ~Say the number of the person you wish to send home within 30 seconds~ \n";
        
        integer i = 0;
        
        for (i = 0; i < intNumAVs; i++)
        {
           AvList += "   " + (string)i + ". " + llKey2Name(llList2String(glstAVList, i)) + "\n";
        }   
    
        // Say List to Owner   
        llOwnerSay(AvList);
    }
}

funAction()
{
    // Remove Listen and Stop Timer
    llListenRemove(gintTempDCC);
    llSetTimerEvent(0);
    
    // Clear List
    glstAVList = [];

    // Notify manager of action
    llOwnerSay(llKey2Name(gkeyAvatar) + " was sent home.");    
       
    // Post the event
    // Events are sent to the zAF_Event Reproter Script, all that needs to be sent is 
    // the Avatars Key, Name, and Number of hours
    // if it is not a ban then the third item would be N/A
    // The first part of the string identifies what happened, the following attributes are
    // accepted, this can be extended by updating the zAF_Event_Reporter script
    // Home
    // Banned
    // Ejected
    // Warned
    
    // Build message string    
    llMessageLinked(1, 666, "Home^" + (string)gkeyAvatar + "^" + llKey2Name(gkeyAvatar) + "^N/A", NULL_KEY);
    
        
     // IM the message to the person sent home
    llInstantMessage(gkeyAvatar,gstrHomeMessage);
}


default
{
    state_entry()
    {
        // Setup the device
        gintDCC = llCeil(llFrand(10000.0));
        
        // Get URL for web site
        llMessageLinked(LINK_SET, 333, "Getting URL", NULL_KEY);
   }

    touch_start(integer total_number)
    {
        funScan();
    }
    
    listen(integer channel, string name, key id, string message)
    {
        if (channel == 0)
        {
            if (id == llGetOwner())
            {
                if (message == "0")
                {
                    // Get Avatar
                    gkeyAvatar = (key)llList2String(glstAVList, (integer)message);
                }
                else if ((integer)message > 0)
                {
                    // Get Avatar
                    gkeyAvatar = (key)llList2String(glstAVList, (integer)message);
                }
                
            funAction();
            // Clean out listen
            llListenRemove(gintTempListen);
            llSetTimerEvent(30);   
           }
        }
    }

    link_message(integer sender_num, integer num, string str, key id)
    {
        if ( num == 334)
        {
            gstrURL = str;

            // Load Messages
            gstrHTTP = llHTTPRequest(gstrURL + "simnet_home.msg",[HTTP_METHOD, "POST"], "");
        }
    }
            
    sensor(integer total_number)
    {
        
        vector pos = llGetPos();
        integer i;
        
        glstAVList = [];
        
        for (i = 0; i < total_number; i++)
        {
            if (llDetectedName(i) == llKey2Name(llGetOwner()))
            {
                // Don't add to the list
            }
            else
            {
                glstAVList += [llDetectedKey(i)];
            }
        }

        // Set AV List Menu
        funListAVs();
    }
    
    
    timer()
    {
        llListenRemove(gintTempListen);
        llListenRemove(gintTempDCC);
        llSetTimerEvent(0);
    }
    
    on_rez(integer start_param)
    {
        llResetScript();    
    }
    
    http_response(key request_id, integer status, list metadata, string body)
    {
        if (request_id == gstrHTTP)
        {
            request_id = NULL_KEY;
            gstrHomeMessage = body;
        }
        
        //llOwnerSay("Ready");
    }

}

